package com.e2e.haiti.infrastructure.surveyor;

import com.e2e.haiti.infrastructure.surveyor.R;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class CustomSqlCursorAdapter extends SimpleCursorAdapter {
    private Context mContext;

    private SqlHelper mDbHelper;
    private Cursor mCurrentCursor;

    public CustomSqlCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, SqlHelper dbHelper) {
        super(context, layout, c, from, to);
        this.mCurrentCursor = c;
        this.mContext = context;
        this.mDbHelper = dbHelper;
    }

    public View getView(int pos, View inView, ViewGroup parent) {
        View v = inView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.structure_item, null);
        }

        if (!this.mCurrentCursor.moveToPosition(pos)) {
            throw new SQLException("CustomSqlCursorAdapter.getView: Unable to move to position: "+pos);
        }

        // Sets structure title
        TextView structureTitle = (TextView) v.findViewById(R.id.structure_title);
        structureTitle.setText(this.mCurrentCursor.getString(this.mCurrentCursor
                .getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NAME)));
        
        //Sets description
        
        TextView structureDescription = (TextView) v.findViewById(R.id.structure_description);
        structureDescription.setText(this.mCurrentCursor.getString(this.mCurrentCursor
                .getColumnIndex(SqlHelper.COLUMN_STRUCTURE_DESC)));
        
        //Sets status        
        TextView structureStatus = (TextView) v.findViewById(R.id.structure_status);
        structureStatus.setText(this.mCurrentCursor.getString(this.mCurrentCursor
                .getColumnIndex(SqlHelper.COLUMN_STRUCTURE_STATUS)));

        close();
        return (v);
    }
    
    public void close() 
    {
        this.mDbHelper.close();
    }
}